package com.ego.cart.properties;

import com.ego.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PublicKey;

@Slf4j
@Data
@ConfigurationProperties(prefix = "ego.jwt")
public class JwtProperties {

    private String pubKeyPath;
    private String cookieName;
    private PublicKey publicKey;

    public JwtProperties() {

    }

    /**
     * 当属性注入后才执行得方法
     * @throws Exception
     */
    @PostConstruct
    public void init() throws Exception {
        try {
            //检查私钥是否存在
            File publicKeyFile = new File(pubKeyPath);

            if (!publicKeyFile.exists()) {
                log.error("公钥不存在，请向相关人员获取公钥");
                throw new RuntimeException();
            }
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            log.error("加载公钥异常",e);
        }
    }

}
